package test2;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author liule
 * @date 2023/8/7 14:37
 * @Desc : 使用httpclient发送get请求
 */
public class HttpClientTest {
    public static void main(String[] args) throws Exception {
        // 使用httpclient发送get请求
        //可关闭的httpclient客户端，相当于你打开的一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlstr = "https://www.baidu.com/";
//        String passwordParam = "123+abc"; // 123 abc
//        passwordParam = URLEncoder.encode(passwordParam, StandardCharsets.UTF_8.name());
//        String urlstr = "https://www.baidu.com/test？userName=aaa&password=" + passwordParam;

        // 加请求头： user-agent和refer请求头的作用
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlstr);
        // user-agent:解决HttpClient被认为不是真人行为
        httpGet.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");
        //防盗链，value :发生防盗链的网站的url
        //httpGet.addHeader("Referer" , "https://www.baidu.com/");

        // 可关闭的响应
        CloseableHttpResponse response = null;
        try {
            response = closeableHttpClient.execute(httpGet);
            // 代表本次请求的成功失败的状态
            StatusLine statusLine = response.getStatusLine();
            if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
                System.out.println("相应成功");
                // 获取响应头
                Header[] allHeaders = response.getAllHeaders();
                for (Header header : allHeaders) {
                    System.out.println("响应头" + header.getName() + "的值: " + header.getValue());
                }

                //获取响应结果 : DecompressingEntity 解压缩
                /**
                 * HttpEntity 不仅可以作为结果，也可以作为请求的参数实体，有很多的实现。
                 */
                HttpEntity entity = response.getEntity();
                Header contentType = entity.getContentType();
                System.out.println("contentType:"+contentType);
                //对HttpEntity操作的工具类
                String toStringSesult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                System.out.println(toStringSesult);
                // 确保流关闭
                EntityUtils.consume(entity);
            } else {
                System.out.println("响应失败，响应码：" + statusLine.getStatusCode());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeableHttpClient != null) {
                closeableHttpClient.close();
            }
            if (response != null) {
                response.close();
            }
        }

    }
}
